﻿#include "fileinfo.h"
#include "ui_fileinfo.h"

FileInfo::FileInfo(const QString &filePath,QWidget *parent) :
    QWidget(parent),
    filePath(filePath),
    ui(new Ui::FileInfo)
{
    ui->setupUi(this);
    QFileInfo fileInfo(filePath);
    QString suffix = fileInfo.suffix();
    ui->fileImg_LB->setStyleSheet(QString(
        "border-image:url(':/background/imagesRes/%1.png') 0 0 0 0 stretch stretch;"
    ).arg(getFileTypeImg(suffix)));
    ui->fileName_LB->setText(fileInfo.fileName());
    ui->fileSize_LB->setText(unitConversion(fileInfo.size()));

    connect(ui->FileInfo_content,&FileInfoContent::fileClicked,[this](){
        QFileInfo fileInfo(this->filePath);
        if(fileInfo.exists()&&fileInfo.size()>0){
            QUrl url = QUrl::fromLocalFile(this->filePath);
            if (!QDesktopServices::openUrl(url)) {
                qDebug() << "Failed to open the file:" << this->filePath;
            }
        }
        else{
            emit downLoadFile();
        }
    });
}

FileInfo::~FileInfo()
{
    delete ui;
}
void FileInfo::updateFileStatus(){
    QFileInfo fileInfo(filePath);
    ui->fileSize_LB->setText(unitConversion(fileInfo.size()));
}
void FileInfo::setFileStatus(const int &percent){
    ui->fileSize_LB->setText(QString("%1 %").arg(percent));
}
